<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>mock_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_labs_mock_mock_test.js.html">mock_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>goog.provide(&#39;goog.labs.mockTest&#39;);
<a name="line16"></a>goog.setTestOnly(&#39;goog.labs.mockTest&#39;);
<a name="line17"></a>
<a name="line18"></a>goog.require(&#39;goog.array&#39;);
<a name="line19"></a>goog.require(&#39;goog.labs.mock&#39;);
<a name="line20"></a>goog.require(&#39;goog.labs.mock.VerificationError&#39;);
<a name="line21"></a>/** @suppress {extraRequire} */
<a name="line22"></a>goog.require(&#39;goog.labs.testing.AnythingMatcher&#39;);
<a name="line23"></a>/** @suppress {extraRequire} */
<a name="line24"></a>goog.require(&#39;goog.labs.testing.GreaterThanMatcher&#39;);
<a name="line25"></a>goog.require(&#39;goog.string&#39;);
<a name="line26"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line27"></a>
<a name="line28"></a>var ParentClass = function() {};
<a name="line29"></a>ParentClass.prototype.method1 = function() {};
<a name="line30"></a>ParentClass.prototype.x = 1;
<a name="line31"></a>ParentClass.prototype.val = 0;
<a name="line32"></a>ParentClass.prototype.incrementVal = function() { this.val++; };
<a name="line33"></a>
<a name="line34"></a>var ChildClass = function() {};
<a name="line35"></a>goog.inherits(ChildClass, ParentClass);
<a name="line36"></a>ChildClass.prototype.method2 = function() {};
<a name="line37"></a>ChildClass.prototype.y = 2;
<a name="line38"></a>
<a name="line39"></a>function testParentClass() {
<a name="line40"></a>  var parentMock = goog.labs.mock.mock(ParentClass);
<a name="line41"></a>
<a name="line42"></a>  assertNotUndefined(parentMock.method1);
<a name="line43"></a>  assertUndefined(parentMock.method1());
<a name="line44"></a>  assertUndefined(parentMock.method2);
<a name="line45"></a>  assertNotUndefined(parentMock.x);
<a name="line46"></a>  assertUndefined(parentMock.y);
<a name="line47"></a>  assertTrue(&#39;Mock should be an instance of the mocked class.&#39;,
<a name="line48"></a>      parentMock instanceof ParentClass);
<a name="line49"></a>}
<a name="line50"></a>
<a name="line51"></a>function testChildClass() {
<a name="line52"></a>  var childMock = goog.labs.mock.mock(ChildClass);
<a name="line53"></a>
<a name="line54"></a>  assertNotUndefined(childMock.method1);
<a name="line55"></a>  assertUndefined(childMock.method1());
<a name="line56"></a>  assertNotUndefined(childMock.method2);
<a name="line57"></a>  assertUndefined(childMock.method2());
<a name="line58"></a>  assertNotUndefined(childMock.x);
<a name="line59"></a>  assertNotUndefined(childMock.y);
<a name="line60"></a>  assertTrue(&#39;Mock should be an instance of the mocked class.&#39;,
<a name="line61"></a>      childMock instanceof ChildClass);
<a name="line62"></a>}
<a name="line63"></a>
<a name="line64"></a>function testParentClassInstance() {
<a name="line65"></a>  var parentMock = goog.labs.mock.mock(new ParentClass());
<a name="line66"></a>
<a name="line67"></a>  assertNotUndefined(parentMock.method1);
<a name="line68"></a>  assertUndefined(parentMock.method1());
<a name="line69"></a>  assertUndefined(parentMock.method2);
<a name="line70"></a>  assertNotUndefined(parentMock.x);
<a name="line71"></a>  assertUndefined(parentMock.y);
<a name="line72"></a>  assertTrue(&#39;Mock should be an instance of the mocked class.&#39;,
<a name="line73"></a>      parentMock instanceof ParentClass);
<a name="line74"></a>}
<a name="line75"></a>
<a name="line76"></a>function testChildClassInstance() {
<a name="line77"></a>  var childMock = goog.labs.mock.mock(new ChildClass());
<a name="line78"></a>
<a name="line79"></a>  assertNotUndefined(childMock.method1);
<a name="line80"></a>  assertUndefined(childMock.method1());
<a name="line81"></a>  assertNotUndefined(childMock.method2);
<a name="line82"></a>  assertUndefined(childMock.method2());
<a name="line83"></a>  assertNotUndefined(childMock.x);
<a name="line84"></a>  assertNotUndefined(childMock.y);
<a name="line85"></a>  assertTrue(&#39;Mock should be an instance of the mocked class.&#39;,
<a name="line86"></a>      childMock instanceof ParentClass);
<a name="line87"></a>}
<a name="line88"></a>
<a name="line89"></a>function testNonEnumerableProperties() {
<a name="line90"></a>  var mockObject = goog.labs.mock.mock({});
<a name="line91"></a>  assertNotUndefined(mockObject.toString);
<a name="line92"></a>  goog.labs.mock.when(mockObject).toString().then(function() {
<a name="line93"></a>    return &#39;toString&#39;;
<a name="line94"></a>  });
<a name="line95"></a>  assertEquals(&#39;toString&#39;, mockObject.toString());
<a name="line96"></a>}
<a name="line97"></a>
<a name="line98"></a>function testBasicStubbing() {
<a name="line99"></a>  var obj = {
<a name="line100"></a>    method1: function(i) {
<a name="line101"></a>      return 2 * i;
<a name="line102"></a>    },
<a name="line103"></a>    method2: function(i, str) {
<a name="line104"></a>      return str;
<a name="line105"></a>    },
<a name="line106"></a>    method3: function(x) {
<a name="line107"></a>      return x;
<a name="line108"></a>    }
<a name="line109"></a>  };
<a name="line110"></a>
<a name="line111"></a>  var mockObj = goog.labs.mock.mock(obj);
<a name="line112"></a>  goog.labs.mock.when(mockObj).method1(2).then(function(i) {return i;});
<a name="line113"></a>
<a name="line114"></a>  assertEquals(4, obj.method1(2));
<a name="line115"></a>  assertEquals(2, mockObj.method1(2));
<a name="line116"></a>  assertUndefined(mockObj.method1(4));
<a name="line117"></a>
<a name="line118"></a>  goog.labs.mock.when(mockObj).method2(1, &#39;hi&#39;).then(function(i) {return &#39;oh&#39;});
<a name="line119"></a>  assertEquals(&#39;hi&#39;, obj.method2(1, &#39;hi&#39;));
<a name="line120"></a>  assertEquals(&#39;oh&#39;, mockObj.method2(1, &#39;hi&#39;));
<a name="line121"></a>  assertUndefined(mockObj.method2(3, &#39;foo&#39;));
<a name="line122"></a>
<a name="line123"></a>  goog.labs.mock.when(mockObj).method3(4).thenReturn(10);
<a name="line124"></a>  assertEquals(4, obj.method3(4));
<a name="line125"></a>  assertEquals(10, mockObj.method3(4));
<a name="line126"></a>  goog.labs.mock.verify(mockObj).method3(4);
<a name="line127"></a>  assertUndefined(mockObj.method3(5));
<a name="line128"></a>}
<a name="line129"></a>
<a name="line130"></a>function testMockFunctions() {
<a name="line131"></a>  function x(i) { return i; }
<a name="line132"></a>
<a name="line133"></a>  var mockedFunc = goog.labs.mock.mockFunction(x);
<a name="line134"></a>  goog.labs.mock.when(mockedFunc)(100).thenReturn(10);
<a name="line135"></a>  goog.labs.mock.when(mockedFunc)(50).thenReturn(25);
<a name="line136"></a>
<a name="line137"></a>  assertEquals(100, x(100));
<a name="line138"></a>  assertEquals(10, mockedFunc(100));
<a name="line139"></a>  assertEquals(25, mockedFunc(50));
<a name="line140"></a>}
<a name="line141"></a>
<a name="line142"></a>function testSpying() {
<a name="line143"></a>  var obj = {
<a name="line144"></a>    method1: function(i) {
<a name="line145"></a>      return 2 * i;
<a name="line146"></a>    },
<a name="line147"></a>    method2: function(i) {
<a name="line148"></a>      return 5 * i;
<a name="line149"></a>    }
<a name="line150"></a>  };
<a name="line151"></a>
<a name="line152"></a>  var spyObj = goog.labs.mock.spy(obj);
<a name="line153"></a>  goog.labs.mock.when(spyObj).method1(2).thenReturn(5);
<a name="line154"></a>
<a name="line155"></a>  assertEquals(2, obj.method1(1));
<a name="line156"></a>  assertEquals(5, spyObj.method1(2));
<a name="line157"></a>  goog.labs.mock.verify(spyObj).method1(2);
<a name="line158"></a>  assertEquals(2, spyObj.method1(1));
<a name="line159"></a>  goog.labs.mock.verify(spyObj).method1(1);
<a name="line160"></a>  assertEquals(20, spyObj.method2(4));
<a name="line161"></a>  goog.labs.mock.verify(spyObj).method2(4);
<a name="line162"></a>}
<a name="line163"></a>
<a name="line164"></a>function testSpyParentClassInstance() {
<a name="line165"></a>  var parent = new ParentClass();
<a name="line166"></a>  var parentMock = goog.labs.mock.spy(parent);
<a name="line167"></a>
<a name="line168"></a>  assertNotUndefined(parentMock.method1);
<a name="line169"></a>  assertUndefined(parentMock.method1());
<a name="line170"></a>  assertUndefined(parentMock.method2);
<a name="line171"></a>  assertNotUndefined(parentMock.x);
<a name="line172"></a>  assertUndefined(parentMock.y);
<a name="line173"></a>  assertTrue(&#39;Mock should be an instance of the mocked class.&#39;,
<a name="line174"></a>      parentMock instanceof ParentClass);
<a name="line175"></a>  var incrementedOrigVal = parent.val + 1;
<a name="line176"></a>  parentMock.incrementVal();
<a name="line177"></a>  assertEquals(&#39;Changes in the spied object should reflect in the spy.&#39;,
<a name="line178"></a>      incrementedOrigVal, parentMock.val);
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>function testSpyChildClassInstance() {
<a name="line182"></a>  var child = new ChildClass();
<a name="line183"></a>  var childMock = goog.labs.mock.spy(child);
<a name="line184"></a>
<a name="line185"></a>  assertNotUndefined(childMock.method1);
<a name="line186"></a>  assertUndefined(childMock.method1());
<a name="line187"></a>  assertNotUndefined(childMock.method2);
<a name="line188"></a>  assertUndefined(childMock.method2());
<a name="line189"></a>  assertNotUndefined(childMock.x);
<a name="line190"></a>  assertNotUndefined(childMock.y);
<a name="line191"></a>  assertTrue(&#39;Mock should be an instance of the mocked class.&#39;,
<a name="line192"></a>      childMock instanceof ParentClass);
<a name="line193"></a>  var incrementedOrigVal = child.val + 1;
<a name="line194"></a>  childMock.incrementVal();
<a name="line195"></a>  assertEquals(&#39;Changes in the spied object should reflect in the spy.&#39;,
<a name="line196"></a>      incrementedOrigVal, childMock.val);
<a name="line197"></a>}
<a name="line198"></a>
<a name="line199"></a>function testVerifyForObjects() {
<a name="line200"></a>  var obj = {
<a name="line201"></a>    method1: function(i) {
<a name="line202"></a>      return 2 * i;
<a name="line203"></a>    },
<a name="line204"></a>    method2: function(i) {
<a name="line205"></a>      return 5 * i;
<a name="line206"></a>    }
<a name="line207"></a>  };
<a name="line208"></a>
<a name="line209"></a>  var mockObj = goog.labs.mock.mock(obj);
<a name="line210"></a>  goog.labs.mock.when(mockObj).method1(2).thenReturn(5);
<a name="line211"></a>
<a name="line212"></a>  assertEquals(5, mockObj.method1(2));
<a name="line213"></a>  goog.labs.mock.verify(mockObj).method1(2);
<a name="line214"></a>  var e = assertThrows(goog.bind(goog.labs.mock.verify(mockObj).method1, 2));
<a name="line215"></a>  assertTrue(e instanceof goog.labs.mock.VerificationError);
<a name="line216"></a>}
<a name="line217"></a>
<a name="line218"></a>function testVerifyForFunctions() {
<a name="line219"></a>  var func = function(i) {
<a name="line220"></a>    return i;
<a name="line221"></a>  };
<a name="line222"></a>
<a name="line223"></a>  var mockFunc = goog.labs.mock.mockFunction(func);
<a name="line224"></a>  goog.labs.mock.when(mockFunc)(2).thenReturn(55);
<a name="line225"></a>  assertEquals(55, mockFunc(2));
<a name="line226"></a>  goog.labs.mock.verify(mockFunc)(2);
<a name="line227"></a>  goog.labs.mock.verify(mockFunc)(lessThan(3));
<a name="line228"></a>
<a name="line229"></a>  var e = assertThrows(goog.bind(goog.labs.mock.verify(mockFunc), 3));
<a name="line230"></a>  assertTrue(e instanceof goog.labs.mock.VerificationError);
<a name="line231"></a>}
<a name="line232"></a>
<a name="line233"></a>
<a name="line234"></a>/**
<a name="line235"></a>* When a function invocation verification fails, it should show the failed
<a name="line236"></a>* expectation call, as well as the recorded calls to the same method.
<a name="line237"></a>*/
<a name="line238"></a>function testVerificationErrorMessages() {
<a name="line239"></a>  var mock = goog.labs.mock.mock({
<a name="line240"></a>    method: function(i) {
<a name="line241"></a>      return i;
<a name="line242"></a>    }
<a name="line243"></a>  });
<a name="line244"></a>
<a name="line245"></a>  // Failure when there are no recorded calls.
<a name="line246"></a>  var e = assertThrows(function() { goog.labs.mock.verify(mock).method(4); });
<a name="line247"></a>  assertTrue(e instanceof goog.labs.mock.VerificationError);
<a name="line248"></a>  var expected = &#39;\nExpected: method(4)\n&#39; +
<a name="line249"></a>      &#39;Recorded: No recorded calls&#39;;
<a name="line250"></a>  assertEquals(expected, e.message);
<a name="line251"></a>
<a name="line252"></a>
<a name="line253"></a>  // Failure when there are recorded calls with ints and functions
<a name="line254"></a>  // as arguments.
<a name="line255"></a>  var callback = function() {};
<a name="line256"></a>  var callbackId = goog.labs.mock.getUid(callback);
<a name="line257"></a>
<a name="line258"></a>  mock.method(1);
<a name="line259"></a>  mock.method(2);
<a name="line260"></a>  mock.method(callback);
<a name="line261"></a>
<a name="line262"></a>  e = assertThrows(function() { goog.labs.mock.verify(mock).method(3); });
<a name="line263"></a>  assertTrue(e instanceof goog.labs.mock.VerificationError);
<a name="line264"></a>
<a name="line265"></a>  expected = &#39;\nExpected: method(3)\n&#39; +
<a name="line266"></a>      &#39;Recorded: method(1),\n&#39; +
<a name="line267"></a>      &#39;          method(2),\n&#39; +
<a name="line268"></a>      &#39;          method(&lt;function #anonymous&#39; + callbackId + &#39;&gt;)&#39;;
<a name="line269"></a>  assertEquals(expected, e.message);
<a name="line270"></a>
<a name="line271"></a>  // With mockFunctions
<a name="line272"></a>  var mockCallback = goog.labs.mock.mockFunction(callback);
<a name="line273"></a>  e = assertThrows(function() { goog.labs.mock.verify(mockCallback)(5);});
<a name="line274"></a>  expected = &#39;\nExpected: #mockFor&lt;#anonymous&#39; + callbackId + &#39;&gt;(5)\n&#39; +
<a name="line275"></a>      &#39;Recorded: No recorded calls&#39;;
<a name="line276"></a>
<a name="line277"></a>  mockCallback(8);
<a name="line278"></a>  goog.labs.mock.verify(mockCallback)(8);
<a name="line279"></a>  assertEquals(expected, e.message);
<a name="line280"></a>
<a name="line281"></a>  // Objects with circular references should not fail.
<a name="line282"></a>  var obj = {x: 1};
<a name="line283"></a>  obj.y = obj;
<a name="line284"></a>
<a name="line285"></a>  mockCallback(obj);
<a name="line286"></a>  e = assertThrows(function() { goog.labs.mock.verify(mockCallback)(5);});
<a name="line287"></a>  assertTrue(e instanceof goog.labs.mock.VerificationError);
<a name="line288"></a>
<a name="line289"></a>  // Should respect string representation of different custom classes.
<a name="line290"></a>  var myClass = function() {};
<a name="line291"></a>  myClass.prototype.toString = function() { return &#39;&lt;superClass&gt;&#39;; };
<a name="line292"></a>
<a name="line293"></a>  var mockFunction = goog.labs.mock.mockFunction(function f() {});
<a name="line294"></a>  mockFunction(new myClass());
<a name="line295"></a>
<a name="line296"></a>  e = assertThrows(function() { goog.labs.mock.verify(mockFunction)(5);});
<a name="line297"></a>  expected = &#39;\nExpected: #mockFor&lt;f&gt;(5)\n&#39; +
<a name="line298"></a>      &#39;Recorded: #mockFor&lt;f&gt;(&lt;superClass&gt;)&#39;;
<a name="line299"></a>  assertEquals(expected, e.message);
<a name="line300"></a>}
<a name="line301"></a>
<a name="line302"></a>
<a name="line303"></a>/**
<a name="line304"></a>* Asserts that the given string contains a list of others strings
<a name="line305"></a>* in the given order.
<a name="line306"></a>*/
<a name="line307"></a>function assertContainsInOrder(str, var_args) {
<a name="line308"></a>  var expected = goog.array.splice(arguments, 1);
<a name="line309"></a>  var indices = goog.array.map(expected, function(val) {
<a name="line310"></a>    return str.indexOf(val);
<a name="line311"></a>  });
<a name="line312"></a>
<a name="line313"></a>  for (var i = 0; i &lt; expected.length; i++) {
<a name="line314"></a>    var msg = &#39;Missing &quot;&#39; + expected[i] + &#39;&quot; from &quot;&#39; + str + &#39;&quot;&#39;;
<a name="line315"></a>    assertTrue(msg, indices[i] != -1);
<a name="line316"></a>
<a name="line317"></a>    if (i &gt; 0) {
<a name="line318"></a>      msg = &#39;&quot;&#39; + expected[i - 1] + &#39;&quot; should come before &quot;&#39; + expected[i] +
<a name="line319"></a>          &#39;&quot; in &quot;&#39; + str + &#39;&quot;&#39;;
<a name="line320"></a>      assertTrue(msg, indices[i] &gt; indices[i - 1]);
<a name="line321"></a>    }
<a name="line322"></a>  }
<a name="line323"></a>}
<a name="line324"></a>
<a name="line325"></a>function testMatchers() {
<a name="line326"></a>  var obj = {
<a name="line327"></a>    method1: function(i) {
<a name="line328"></a>      return 2 * i;
<a name="line329"></a>    },
<a name="line330"></a>    method2: function(i) {
<a name="line331"></a>      return 5 * i;
<a name="line332"></a>    }
<a name="line333"></a>  };
<a name="line334"></a>
<a name="line335"></a>  var mockObj = goog.labs.mock.mock(obj);
<a name="line336"></a>
<a name="line337"></a>  goog.labs.mock.when(mockObj).method1(greaterThan(4)).thenReturn(100);
<a name="line338"></a>  goog.labs.mock.when(mockObj).method1(lessThan(4)).thenReturn(40);
<a name="line339"></a>
<a name="line340"></a>  assertEquals(100, mockObj.method1(5));
<a name="line341"></a>  assertEquals(100, mockObj.method1(6));
<a name="line342"></a>  assertEquals(40, mockObj.method1(2));
<a name="line343"></a>  assertEquals(40, mockObj.method1(1));
<a name="line344"></a>  assertUndefined(mockObj.method1(4));
<a name="line345"></a>}
<a name="line346"></a>
<a name="line347"></a>function testMatcherVerify() {
<a name="line348"></a>  var obj = {
<a name="line349"></a>    method: function(i) {
<a name="line350"></a>      return 2 * i;
<a name="line351"></a>    }
<a name="line352"></a>  };
<a name="line353"></a>
<a name="line354"></a>  // Using spy objects.
<a name="line355"></a>  var spy = goog.labs.mock.spy(obj);
<a name="line356"></a>
<a name="line357"></a>  spy.method(6);
<a name="line358"></a>
<a name="line359"></a>  goog.labs.mock.verify(spy).method(greaterThan(4));
<a name="line360"></a>  var e = assertThrows(
<a name="line361"></a>      goog.bind(goog.labs.mock.verify(spy).method, lessThan(4)));
<a name="line362"></a>  assertTrue(e instanceof goog.labs.mock.VerificationError);
<a name="line363"></a>
<a name="line364"></a>  // Using mocks
<a name="line365"></a>  var mockObj = goog.labs.mock.mock(obj);
<a name="line366"></a>
<a name="line367"></a>  mockObj.method(8);
<a name="line368"></a>
<a name="line369"></a>  goog.labs.mock.verify(mockObj).method(greaterThan(7));
<a name="line370"></a>  var e = assertThrows(
<a name="line371"></a>      goog.bind(goog.labs.mock.verify(mockObj).method, lessThan(7)));
<a name="line372"></a>  assertTrue(e instanceof goog.labs.mock.VerificationError);
<a name="line373"></a>}
<a name="line374"></a>
<a name="line375"></a>function testMatcherVerifyColission() {
<a name="line376"></a>  var obj = {
<a name="line377"></a>    method: function(i) {
<a name="line378"></a>      return 2 * i;
<a name="line379"></a>    }
<a name="line380"></a>  };
<a name="line381"></a>  var mockObj = goog.labs.mock.mock(obj);
<a name="line382"></a>
<a name="line383"></a>  goog.labs.mock.when(mockObj).method(5).thenReturn(100);
<a name="line384"></a>  assertNotEquals(100, mockObj.method(greaterThan(2)));
<a name="line385"></a>}
<a name="line386"></a>
<a name="line387"></a>function testMatcherVerifyColissionBetweenMatchers() {
<a name="line388"></a>  var obj = {
<a name="line389"></a>    method: function(i) {
<a name="line390"></a>      return 2 * i;
<a name="line391"></a>    }
<a name="line392"></a>  };
<a name="line393"></a>  var mockObj = goog.labs.mock.mock(obj);
<a name="line394"></a>
<a name="line395"></a>  goog.labs.mock.when(mockObj).method(anything()).thenReturn(100);
<a name="line396"></a>
<a name="line397"></a>  var e = assertThrows(
<a name="line398"></a>      goog.bind(goog.labs.mock.verify(mockObj).method, anything()));
<a name="line399"></a>  assertTrue(e instanceof goog.labs.mock.VerificationError);
<a name="line400"></a>}
<a name="line401"></a>
<a name="line402"></a>function testVerifyForUnmockedMethods() {
<a name="line403"></a>  var Task = function() {};
<a name="line404"></a>  Task.prototype.run = function() {};
<a name="line405"></a>
<a name="line406"></a>  var mockTask = goog.labs.mock.mock(Task);
<a name="line407"></a>  mockTask.run();
<a name="line408"></a>
<a name="line409"></a>  goog.labs.mock.verify(mockTask).run();
<a name="line410"></a>}
<a name="line411"></a>
<a name="line412"></a>function testFormatMethodCall() {
<a name="line413"></a>  var formatMethodCall = goog.labs.mock.formatMethodCall_;
<a name="line414"></a>  assertEquals(&#39;alert()&#39;, formatMethodCall(&#39;alert&#39;));
<a name="line415"></a>  assertEquals(&#39;sum(2, 4)&#39;, formatMethodCall(&#39;sum&#39;, [2, 4]));
<a name="line416"></a>  assertEquals(&#39;sum(&quot;2&quot;, &quot;4&quot;)&#39;, formatMethodCall(&#39;sum&#39;, [&#39;2&#39;, &#39;4&#39;]));
<a name="line417"></a>  assertEquals(&#39;call(&lt;function unicorn&gt;)&#39;,
<a name="line418"></a>      formatMethodCall(&#39;call&#39;, [function unicorn() {}]));
<a name="line419"></a>
<a name="line420"></a>  var arg = {x: 1, y: {hello: &#39;world&#39;}};
<a name="line421"></a>  assertEquals(&#39;call(&#39; + goog.labs.mock.formatValue_(arg) + &#39;)&#39;,
<a name="line422"></a>      formatMethodCall(&#39;call&#39;, [arg]));
<a name="line423"></a>}
<a name="line424"></a>
<a name="line425"></a>function testGetFunctionName() {
<a name="line426"></a>  var f1 = function() {};
<a name="line427"></a>  var f2 = function() {};
<a name="line428"></a>  var named = function myName() {};
<a name="line429"></a>
<a name="line430"></a>  assert(goog.string.startsWith(
<a name="line431"></a>      goog.labs.mock.getFunctionName_(f1), &#39;#anonymous&#39;));
<a name="line432"></a>  assert(goog.string.startsWith(
<a name="line433"></a>      goog.labs.mock.getFunctionName_(f2), &#39;#anonymous&#39;));
<a name="line434"></a>  assertNotEquals(
<a name="line435"></a>      goog.labs.mock.getFunctionName_(f1), goog.labs.mock.getFunctionName_(f2));
<a name="line436"></a>  assertEquals(&#39;myName&#39;, goog.labs.mock.getFunctionName_(named));
<a name="line437"></a>}
<a name="line438"></a>
<a name="line439"></a>function testFormatObject() {
<a name="line440"></a>  var obj, obj2, obj3;
<a name="line441"></a>
<a name="line442"></a>  obj = {x: 1};
<a name="line443"></a>  assertEquals(
<a name="line444"></a>      &#39;{&quot;x&quot;:1 _id:&#39; + goog.labs.mock.getUid(obj) + &#39;}&#39;,
<a name="line445"></a>      goog.labs.mock.formatValue_(obj)
<a name="line446"></a>  );
<a name="line447"></a>  assertEquals(&#39;{&quot;x&quot;:1}&#39;, goog.labs.mock.formatValue_(obj, false /* id */));
<a name="line448"></a>
<a name="line449"></a>  obj = {x: &#39;hello&#39;};
<a name="line450"></a>  assertEquals(
<a name="line451"></a>      &#39;{&quot;x&quot;:&quot;hello&quot; _id:&#39; + goog.labs.mock.getUid(obj) + &#39;}&#39;,
<a name="line452"></a>      goog.labs.mock.formatValue_(obj)
<a name="line453"></a>  );
<a name="line454"></a>  assertEquals(&#39;{&quot;x&quot;:&quot;hello&quot;}&#39;,
<a name="line455"></a>      goog.labs.mock.formatValue_(obj, false /* id */));
<a name="line456"></a>
<a name="line457"></a>  obj3 = {};
<a name="line458"></a>  obj2 = {y: obj3};
<a name="line459"></a>  obj3.x = obj2;
<a name="line460"></a>  assertEquals(
<a name="line461"></a>      &#39;{&quot;x&quot;:{&quot;y&quot;:&lt;recursive/dupe obj_&#39; + goog.labs.mock.getUid(obj3) + &#39;&gt; &#39; +
<a name="line462"></a>      &#39;_id:&#39; + goog.labs.mock.getUid(obj2) + &#39;} &#39; +
<a name="line463"></a>      &#39;_id:&#39; + goog.labs.mock.getUid(obj3) + &#39;}&#39;,
<a name="line464"></a>      goog.labs.mock.formatValue_(obj3)
<a name="line465"></a>  );
<a name="line466"></a>  assertEquals(&#39;{&quot;x&quot;:{&quot;y&quot;:&lt;recursive/dupe&gt;}}&#39;,
<a name="line467"></a>      goog.labs.mock.formatValue_(obj3, false /* id */)
<a name="line468"></a>  );
<a name="line469"></a>
<a name="line470"></a>
<a name="line471"></a>  obj = {x: function y() {} };
<a name="line472"></a>  assertEquals(&#39;{&quot;x&quot;:&lt;function y&gt; _id:&#39; + goog.labs.mock.getUid(obj) + &#39;}&#39;,
<a name="line473"></a>      goog.labs.mock.formatValue_(obj));
<a name="line474"></a>  assertEquals(&#39;{&quot;x&quot;:&lt;function y&gt;}&#39;,
<a name="line475"></a>      goog.labs.mock.formatValue_(obj, false /* id */));
<a name="line476"></a>
<a name="line477"></a>}
<a name="line478"></a>
<a name="line479"></a>function testGetUid() {
<a name="line480"></a>  var obj1 = {};
<a name="line481"></a>  var obj2 = {};
<a name="line482"></a>  var func1 = function() {};
<a name="line483"></a>  var func2 = function() {};
<a name="line484"></a>
<a name="line485"></a>  assertNotEquals(goog.labs.mock.getUid(obj1), goog.labs.mock.getUid(obj2));
<a name="line486"></a>  assertNotEquals(goog.labs.mock.getUid(func1), goog.labs.mock.getUid(func2));
<a name="line487"></a>  assertNotEquals(goog.labs.mock.getUid(obj1), goog.labs.mock.getUid(func2));
<a name="line488"></a>  assertEquals(goog.labs.mock.getUid(obj1), goog.labs.mock.getUid(obj1));
<a name="line489"></a>  assertEquals(goog.labs.mock.getUid(func1), goog.labs.mock.getUid(func1));
<a name="line490"></a>}
</pre>


</body>
</html>
